import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'common/util/store_util.dart';
import 'page/assign_work_order/assign_work_order.dart';
import 'page/assign_work_order/assign_work_order_detail.dart';
import 'page/home/home.dart';
import 'page/home/launch_screen.dart';
import 'page/live_audit/live_audit.dart';
import 'page/live_audit/live_audit_detail.dart';
import 'page/role_management/role_management.dart';
import 'page/room_management/room_management.dart';
import 'page/site_management/site_add.dart';
import 'page/site_management/site_detail.dart';
import 'page/site_management/site_management.dart';
import 'page/staff_management/staff_management.dart';
import 'page/stock_order/stock_order.dart';
import 'page/supplier_audit/supplier_audit.dart';
import 'page/user/login.dart';
import 'page/user/user_info.dart';
import 'page/work_audit/work_audit.dart';
import 'page/error/not_found.dart';

class RouterTable {
  static String loginPath = '/login';
  static String homePath = '/home';
  static String notFoundPath = '/notFound';

  static Widget homePage = const LaunchScreenPage();

  static List<String> whitelist = ['/login', '/notFound'];

  static Map<String, WidgetBuilder> routeTables = {
    "/notFound": (context) => const NotFoundPage(),
    "/home": (context) => const HomePage(),
    "/login": (context) => const LoginPage(),
    "/userInfo": (context) => const UserInfoPage(),
    "/siteManagement": (context) => const SiteManagementPage(),
    "/siteDetail": (context, {arguments}) =>
        SiteDetailPage(arguments: arguments),
    "/siteAdd": (context, {arguments}) => SiteAddPage(arguments: arguments),
    "/staffManagement": (context) => const StaffManagementPage(),
    "/roleManagement": (context) => const RoleManagementPage(),
    "/liveAudit": (context) => const LiveAuditPage(),
    "/liveAuditDetail": (context, {arguments}) =>
        LiveAuditDetailPage(arguments: arguments),
    "/roomManagement": (context) => const RoomManagementPage(),
    "/workAudit": (context, {arguments}) => WorkAuditPage(arguments: arguments),
    "/stockOrder": (context, {arguments}) =>
        StockOrderPage(arguments: arguments),
    "/assignWorkOrder": (context) => const AssignWorkOrderPage(),
    "/assignWorkOrderDetail": (context, {arguments}) =>
        AssignWorkOrderDetailPage(arguments: arguments),
    "/supplierAudit": (context) => const SupplierAuditPage(),
  };

  static Route onGenerateRoute<T extends Object>(RouteSettings settings) {
    String routeName = routeBeforeHook(settings);
    final Function? pageBuilder = routeTables[routeName];
    if (routeName == notFoundPath) {
      return CupertinoPageRoute(builder: (context) => pageBuilder!(context));
    }

    if (null != pageBuilder) {
      return CupertinoPageRoute(
        builder: (context) {
          return null != settings.arguments
              ? pageBuilder(context, arguments: settings.arguments)
              : pageBuilder(context);
        },
        settings: settings,
      );
    } else {
      return CupertinoPageRoute(builder: (context) => pageBuilder!(context));
    }
  }

  static String routeBeforeHook(RouteSettings settings) {
    String name = settings.name!;

    if (routeTables[name] == null) {
      return notFoundPath;
    }

    if (whitelist.indexOf(name) > 0) {
      return name;
    }

    final token = StoreUtil.getToken();
    if (token != '') {
      return name;
    }

    return loginPath;
  }
}
